package com.javaweb.dao.impl;

import com.javaweb.dao.CategoryDao;
import com.javaweb.model.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

@Repository
public class CategoryDaoImpl implements CategoryDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Category category) {
        if (ObjectUtils.isEmpty(category) || ObjectUtils.isEmpty(category.getName())) {
            return 0; // Insert failed
        }

        String sql = "INSERT INTO tb_category (name, `order`, parent_id, create_time) VALUES (?, ?, ?, ?)";
        Object[] args = new Object[]{
                category.getName(),
                category.getOrder(),
                category.getParentId(),
                new java.sql.Date(category.getCreateTime().getTime())
        };

        return jdbcTemplate.update(sql, args) > 0 ? 1 : 0; // Return insert result
    }
}
